package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.model.domain.Question;
import com.tanhua.model.domain.Settings;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResultVo;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.server.interceptor.UserHolderThreadLocal;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

/**
 * @Description: test
 * @Create by: JJ菜菜
 * @Date: 2021/11/30 0:02
 */

@Service
public class SettingsService {


    @DubboReference
    private QuestionApi questionApi;

    @DubboReference
    private SettingsApi settingsApi;

    @DubboReference
    private BlackListApi blackListApi;

    /**
     * 查询用户通过设置
     *
     * @return
     */
    public SettingsVo settings() {

        // 获取手机号和用户id
        Long userId = UserHolderThreadLocal.getId();
        String phone = UserHolderThreadLocal.getMobial();
        SettingsVo vo = new SettingsVo();
        vo.setId(userId);
        vo.setPhone(phone);
        // 调用questionApi查询问题表
        Question question = questionApi.findByUserId(userId);
        // 如果问题为空就设置为 ""
        vo.setStrangerQuestion(question != null ? question.getTxt() : "你喜欢我啥");

        // 查询Settings表
        Settings settings = settingsApi.findByUserId(userId);
        if (settings != null) {
            vo.setGonggaoNotification(settings.getGonggaoNotification());
            vo.setLikeNotification(settings.getLikeNotification());
            vo.setPinglunNotification(settings.getPinglunNotification());
        }

        return vo;
    }

    // 问题保存
    public void saveQuestion(Long userId, String content) {

        Question question = new Question();
        question.setUserId(userId);
        question.setTxt(content);
        // 查询改用户的问题是否存在
        Question question1 = questionApi.findByUserId(userId);
        // 如果问题为空就保存，如果不为空就修改
        if (question1 == null) {
            questionApi.save(question);
        } else {
            questionApi.updateByUserId(question);
        }

    }

    /**
     * 推送开关状态设置
     *
     * @param settings
     */
    public void updateByUserId(Settings settings) {

        Settings settings2 = settingsApi.findByUserId(settings.getUserId());

        // 有数据就修改，没有数据就保存
        if (settings2 == null) {
            settingsApi.save(settings);
        } else {
            settingsApi.updateByUserId(settings);
        }
    }

    /**
     * 黑名单分页查询
     * @param page
     * @param pageSize
     * @param userId
     * @return
     */
    public PageResultVo selectByPage(Integer page, Integer pageSize, Long userId) {

        // 分页查询
        IPage<UserInfo> iPage = blackListApi.findByPage(page, pageSize, userId);
        // 封装vo数据返回controller
        Integer total = Math.toIntExact(iPage.getTotal());
        PageResultVo vo = new PageResultVo(page, pageSize, total, iPage.getRecords());
        return vo;
    }

    public void deleteById(Long uid) {

        Long userId = UserHolderThreadLocal.getId();
        blackListApi.delete(uid, userId);
    }
}
